﻿
@{
    ViewBag.Title = "Index";
}

<div class="panel-body" style="padding-bottom:0px;">
    <div class="panel panel-default">
        <div class="panel-heading">查询条件</div>
        <div class="panel-body">
            <form id="formSearch" class="form-horizontal">
                <div class="form-group" style="margin-top:15px">
                    <label class="control-label col-sm-1" for="Name">流程名称</label>
                    <div class="col-sm-3">
                        <input type="text" class="form-control" id="Name">
                    </div>
                    <label class="control-label col-sm-1" for="Code">流程代码</label>
                    <div class="col-sm-3">
                        <input type="text" class="form-control" id="Code">
                    </div>
                    <div class="col-sm-4" style="text-align:left;">
                        <button type="button" style="margin-left:50px" id="btn_query" class="btn btn-primary" onclick="QueryData()">查询</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div id="toolbar" class="btn-group">
        <button id="btn_add" type="button" class="btn btn-default" data-target="#modal" data-toggle="modal">
            <span class="glyphicon glyphicon-plus" id="addmo" aria-hidden="true"></span> 添加模板
        </button>
        <button id="btn_edit" type="button" class="btn btn-default" onclick="up()">
            <span class="glyphicon glyphicon-pencil" aria-hidden="true" ></span>修改
        </button>
        <button id="btn_delete" type="button" class="btn btn-default" onclick="del()">
            <span class="glyphicon glyphicon-remove"></span>删除
        </button>
        <button id="btn_delete" type="button" class="btn btn-default" onclick="downLoadExcle()">
            <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>导出所有
        </button>
    </div>

    <table id="tb1"></table>

    @*添加模板*@
    <div class="modal inmodal fade" id="modal" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">

                <div class="modal-body">

                    <form class="form-horizontal" role="form">
                        <div class="form-group">
                            <label for="firstname" class="col-sm-2 control-label">流程名称</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="CodeName" placeholder="">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="lastname" class="col-sm-2 control-label">流程代码</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="CodeId" placeholder="">
                            </div>
                        </div>
                        <div class="form-group" style="margin-left:15px">
                            <div class="col-sm-10">
                                <input type="button" class="btn btn-primary" onclick="addBu()" value="添加步骤" id="add">
                            </div>
                        </div>
                        <ul id="addLi">
                            @*<li style="display:inline-block">
                                    <div style="background-color:#F8F8FF;width:170px;height:170px">
                                        <label class="lab">流程 1</label>
                                        <input type="hidden" class="id" value="" />
                                        <br />
                                        <br />
                                        <br />
                                        <br />
                                        <label>审批人:</label><label class="name"></label>
                                        <br />
                                        <input type="button" class="ad" style="margin-top:30px" value="添加审批人" onclick="addShen()" />
                                        <input type="button" class="btnon" style="margin-left:5px" onclick="romver()" value="删除" />
                                    </div>
                                </li>*@
                        </ul>

                    </form>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-white" data-dismiss="modal" id="guan">关闭</button>
                    <button type="button" class="btn btn-primary" id="addshu">保存更改</button>
                </div>
            </div>

        </div>
    </div>


    @*添加审批人*@
    <div class="modal inmodal fade" id="myModal6" tabindex="-1" role="dialog" aria-hidden="true" style="margin-top:10%">
        <div class="modal-dialog modal-sm">
            <div class="modal-content">
                <div class="modal-body" style="margin-top:10%;text-align:center">

                    <form autocomplete="off">
                        <div id="div_txt">

                            <!--要模糊匹配的文本框-->
                            <label>审批人:</label>@*<input type="text" id="txt1" class="layui-input" />*@
                            <select id="div_items">
                                @foreach (var item in ViewBag.staff)
                                {
                                    <option class="div_item" value="@item.Id">@item.Name</option>
                                }
                            </select>
                            <input type="hidden" id="index" value="" />
                        </div>
                    </form>

                </div>
                <div class="modal-footer" style="text-align:center">
                    <input type="hidden" name="Id" value="" />
                    <button type="button" class="btn btn-white" id="exid" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" onclick="adshen()">保存更改</button>
                </div>
            </div>
        </div>
    </div>


</div>

@section Scripts{

    <script src="~/Content/layer/layer/layer.js"></script>
    <link href="~/Content/layer/layer/mobile/need/layer.css" rel="stylesheet" />

    <script src="~/Content/bootstraptable/dist/bootstrap-table.js"></script>
    <script src="~/Content/bootstraptable/dist/locale/bootstrap-table-zh-CN.js"></script>
    <link href="~/Content/bootstraptable/dist/bootstrap-table.css" rel="stylesheet" />

    <script>
        var a = 0;
        $(function () {
            ShowTable();

        })

        function addBu() {
            a++;
            var str = "<li style=\"display: inline-block\"><div style =\"background-color:#F8F8FF;width:170px;height:170px;;margin-right:5px\" ><label class=\"lab\">流程 " + a + "</label><input type=\"hidden\" class=\"id\" value=" + a + " /><br /><br/><br/><br/><label>审批人:</label><label class=\"name\"></label><br/><input type=\"button\" class=\"ad\" style=\"margin-top:30px\" value=\"添加审批人\" data-toggle=\"modal\" data-target=\"#myModal6\" onclick=\"addShen(" + a + ")\" /><input type=\"button\" class=\"btnon\" style=\"margin-left:5px\" onclick=\"romver(" + a + ")\" value=\"删除\" /></div ></li >";
            $("#addLi").append(str);
        }
        function romver(b) {

            var i = 1;
            var k = 1;
            var l = 1;
            var j = 0;

            //删除某一个标签
            $("#addLi li").eq(b - 1).remove();

            //替换标签
            $(".lab").each(function () {

                $(this).replaceWith("<label class=\"lab\">流程 " + i + "</label>");
                i++;
            })

            $(".btnon").each(function () {
                $(this).replaceWith("<input type=\"button\" class=\"btnon\" style=\"margin-left:5px\" onclick=\"romver(" + k + ")\" value=\"删除\" />");
                k += 1;
            })
            $(".ad").each(function () {

                $(this).replaceWith("<input type=\"button\" class=\"ad\" style=\"margin-top:30px\" data-toggle=\"modal\" data-target=\"#myModal6\" value=\"添加审批人\" onclick=\"addShen(" + l + ")\" />");
                l += 1;
            })
            a = i - 1;
        }
        function addShen(index) {
            $("#index").val(index);
        }
        function adshen() {
            $(".name").eq(Number($("#index").val()-1)).text($("#div_items").find("option:selected").text());
            $(".id").eq(Number($("#index").val() - 1)).val($("#div_items").val())

            //关闭
            $("#exid").click();
        }
        //修改
        function up() {
            var data = $("#tb1").bootstrapTable('getSelections');
            if (data == "") {
                layer.msg("请选择删除的内容");
                return false;
            } else if (data.length != 1) {
                layer.msg("请选择一个进行修改");
                return false;
            }

            $.ajax({
                url: "/ProcessItem/selectUp"
                , data: {
                    id: data[0].Id
                }
                , success: function (res) {
                    $("#addmo").click();
                    $("#CodeName").val(data[0].Name);
                    $("#CodeId").val(data[0].Code);
                    var str = "";
                    for (var i = 0; i < res.length; i++) {
                        str += "<li style=\"display: inline-block\"><div style =\"background-color:#F8F8FF;width:170px;height:170px;;margin-right:5px\" ><label class=\"lab\">流程 " + res[i].StepOrder + "</label><input type=\"hidden\" class=\"id\" value=" + res[i].WaitForExecutionStaffId + " /><br /><br/><br/><br/><label>审批人:</label><label class=\"name\">" + res[i].staffName + "</label><br/><input type=\"button\" class=\"ad\" style=\"margin-top:30px\" value=\"添加审批人\" data-toggle=\"modal\" data-target=\"#myModal6\" onclick=\"addShen(" + res[i].StepOrder + ")\" /><input type=\"button\" class=\"btnon\" style=\"margin-left:5px\" onclick=\"romver(" + res[i].StepOrder + ")\" value=\"删除\" /></div ></li >";
                    }
                    $("#addLi").append(str);
                    a = res.length;
                    $("input[name='Id']").val(data[0].Id);
                }
            })
        }
        //删除
        function del() {

            var strid = [];
            var data = $("#tb1").bootstrapTable('getSelections');
            for (var i = 0; i < data.length; i++) {
                strid.push(data[i].Id);
            }
            if (strid.length == 0) {
                layer.msg("请选择删除的内容");
                return false;
            }
            layer.msg('是否确认删除？', {
                time: 0 //不自动关闭
                , icon: 3
                , btn: ['确定', '取消']
                , yes: function (index) {
                    layer.close(index);

                    $.ajax({
                        url: "/ProcessItem/Del",
                        data: {
                            Id: strid.toString()
                        },
                        success: function (res) {
                            if (res) {
                                layer.msg("成功");
                                QueryData();
                            }
                            else {
                               layer.msg("失败");
                            }
                        }
                    })
                }
            });
        }

        $("#addshu").click(function () {

            var liuCodeName = $("#CodeName").val();
            var liuCode = $("#CodeId").val();
            var i = 0;
            var str = [];
            var strId = [];


            if (liuCodeName == "") {
                layer.msg("流程名称不能为空!");
                return false;
            }

            if (liuCode == "") {
                layer.msg("流程代码不能为空!");
                return false;
            }


            $(".name").each(function () {
                if ($(this).text() == "") {
                    i++;
                }
                else {
                    str.push($(this).text());
                }
            })

            $(".id").each(function () {
                strId.push($(this).val());
            })
            if (i > 0) {
                layer.msg("审批人不能为空!");
                return false;
            }



            $.ajax({
                url: "/ProcessItem/Add_up",
                data: {
                    //row: str.toString() == "" ? "-1" : str.toString(),
                    Name: liuCodeName,
                    Code: liuCode,
                    Id: $("input[name='Id']").val(),
                    strid: strId.toString()
                },
                success: function (data) {
                    if (data == (strId.length + 1)) {
                        $("#guan").click();
                        layer.msg("成功");
                        QueryData();
                    }
                    else {
                        layer.msg("失败");
                    }
                }
            })
        })
        $("#guan").click(function () {
            window.location.href = "/ProcessItem/index";
        })
        function QueryData() {

            $("#tb1").bootstrapTable('refresh');
        }
        function ShowTable() {
            $('#tb1').bootstrapTable({
                url: '/ProcessItem/ShowPageList',         //请求后台的URL（*）
                method: 'get',                      //请求方式（*）
                toolbar: '#toolbar',                //工具按钮用哪个容器
                striped: true,                      //是否显示行间隔色
                cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                pagination: true,                   //是否显示分页（*）
                sortable: true,                     //是否启用排序
                sortOrder: "asc",                   //排序方式
                queryParams: function (params) {

                    params.name = $("#Name").val();
                    params.code = $("#Code").val();
                    return params;
                },
                sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
                pageNumber: 1,                       //初始化加载第一页，默认第一页
                pageSize: 2,                       //每页的记录行数（*）
                pageList: [2, 25, 50, 100],        //可供选择的每页的行数（*）
                search: false,                       //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
                strictSearch: true,
                showColumns: true,                  //是否显示所有的列
                showRefresh: false,                  //是否显示刷新按钮
                minimumCountColumns: 1,             //最少允许的列数
                clickToSelect: true,                //是否启用点击选中行
                //height: 500,                        //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
                uniqueId: "Id",                     //每一行的唯一标识，一般为主键列
                showToggle: false,                    //是否显示详细视图和列表视图的切换按钮
                cardView: false,                    //是否显示详细视图
                detailView: true,                   //是否显示父子表
                columns: [{
                    checkbox: true
                }, {
                    field: 'Name',
                    sortable: true,  //排序开启
                    title: '流程名称'
                }, {
                    field: 'Code',
                    sortable: true,
                    title: '流程代码'
                }]
                //,
                //onExpandRow: function (index, row, $detail) {
                //    showSubTable(index, row, $detail);
                //}
            });
        }
    </script>
}

